package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.annotation.Autofill;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
      PageResult pageResult = setmealService.page(setmealPageQueryDTO);
      return Result.success(pageResult);

    }
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result status(@PathVariable Integer status,Long id) {
        setmealService.status(status,id);
        return Result.success();
    }
   @PostMapping()
   @CacheEvict(cacheNames ="setmealCache",key = "#setmealDTO.categoryId")
    public Result add(@RequestBody SetmealDTO setmealDTO){
              setmealService.add(setmealDTO);
        return Result.success();
   }
   @GetMapping("/{id}")
    public Result categoryById(@PathVariable Long id){
       SetmealVO setmealVOResult= setmealService.categoryById(id);
        return Result.success(setmealVOResult);
   }
   @DeleteMapping
   @CacheEvict(cacheNames = "setmealCache",allEntries = true)
   public Result deleteAll(Long[] ids){
        setmealService.deleteAll(Arrays.asList(ids));
        return Result.success();

   }
   @PutMapping
   @CacheEvict(cacheNames = "setmealCache",allEntries = true)
   public Result upDate(@RequestBody SetmealDTO setmealDTO){
        setmealService.upDate(setmealDTO);
        return Result.success();
   }

}